/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.server;

import org.lifebudget.app.client.mvc.model.Category;
import org.lifebudget.app.client.mvc.model.LoginInfo;
import org.lifebudget.app.client.service.CategoryService;
import org.lifebudget.app.client.service.LoginService;
import org.lifebudget.app.persistence.repository.CategoryRepository;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The Class LoginServiceImple implements the {@link LoginService} interface
 * and provides server-side functionality for GWT RPC.
 */
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {
	
	/** The category repository. */
	private CategoryRepository categoryRepo;

	/**
	 * Instantiates a new login service.
	 */
	public LoginServiceImpl() {
		categoryRepo = new CategoryRepository();
	}

	/* (non-Javadoc)
	 * @see org.lifebudget.app.client.service.LoginService#login(java.lang.String)
	 */
	@Override
	public LoginInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		LoginInfo loginInfo = new LoginInfo();

		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));

			newUserSetup();
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

	/**
	 * New user setup.
	 */
	private void newUserSetup() {
		// check to see if the user has has a category defined
		// TODO create a users table and store whether the user has been
		// initialized or not.
		// TODO put a method in categoryRepo called getDefaultCategory, so we
		// don't have to get all of them
		if (categoryRepo.getAllCategories(true).size() == 0) {
			Category c = new Category("Default",
					"Default category created by the system to contain any overflow from paychecks.  Cannot be deleted.");
			c.setArchived(false);
			c.setDefaultCategory(true);
			categoryRepo.addCategory(c);
		}
	}

}
